<!--
 * @Description: 
 * @Author: linjz
 * @Date: 2021-06-25 10:25:00
 * @LastEditors: linjz
 * @LastEditTime: 2021-06-28 17:24:59
-->
<template>
  <!-- 顾客账户管理 -->
  <div>
    <!-- value-format="timestamp"设置事件日期选择器最终的值的格式 -->
    <div>
      <!-- {{ dataValue }}---{{ searchForm }} -->
      <el-form :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item>
          <el-date-picker
            value-format="timestamp"
            v-model="dataValue"
            type="datetimerange"
            :picker-options="pickerOptions"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-select
            v-model="searchForm.type"
            placeholder="请选择类型"
            clearable
          >
            <el-option label="充值" value="充值"></el-option>
            <el-option label="消费" value="消费"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select
            v-model="searchForm.status"
            placeholder="请选择状态"
            clearable
          >
            <el-option label="正常" value="正常"></el-option>
            <el-option label="未审核" value="未审核"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select
            v-model="searchForm.userId"
            placeholder="顾客编号"
            clearable
          >
            <el-option
              v-for="item in customerData"
              :key="item.id"
              :label="item.username"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="queryDataAccount" type="primary">查询</el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 表格 -->
    <div>
      <el-table :data="tableData" stripe style="width: 100%">
        <el-table-column prop="id" label="编号" width="80" align="center">
        </el-table-column>
        <el-table-column prop="transferMoney" label="交易金额" align="center">
        </el-table-column>
        <el-table-column label="交易时间" align="center">
          <template slot-scope="scope">
            {{ comment(scope.row.transferTime).format("yyyy-MM-DD hh:mm:ss") }}
          </template>
        </el-table-column>
        <el-table-column prop="type" label="交易类型" align="center">
        </el-table-column>
        <el-table-column label="状态" align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status == '正常'" type="success"
              >正常</el-tag
            >
            <el-tag v-else type="info">未审核</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="description" label="描述" align="center">
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页 -->
    <div class="page">
      <el-pagination
        :current-page="params.page"
        :page-sizes="[2, 5, 10, 15, 20]"
        :page-size="params.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script>
import { pageQueryCustomerAccount } from "@/api/account";
import { pageQuery as pageQueryCustomer } from "@/api/user";
import comment from "moment";

export default {
  data() {
    return {
      // 时间数组
      dataValue: [],
      // 分页查询
      params: {
        page: 1,
        pageSize: 10,
      },
      // 条数
      total: "",
      // 搜索表单的数据
      searchForm: {},
      // 顾客数据
      customerData: {},
      // 时间处理
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      // 表格的数据
      tableData: {},
    };
  },
  computed: {},
  watch: {
    // 深度监听分页参数
    params: {
      handler() {
        this.queryDataAccount();
      },
      deep: true,
    },
  },
  methods: {
    // 获取顾客信息
    async pageQueryCustomerData() {
      let res = await pageQueryCustomer({ page: 1, pageSize: 8888, roleId: 6 });
      // console.log(res);
      this.customerData = res.data.list;
    },
    // 分页获取系统账单信息，在系统账单页面一上来，就要发请求获取数据，调用queryDataAccount方法。如果是点击了系统账单页面的查询按钮，也调用queryDataAccount，分页发生变化的时候，也是调用queryDataAccount发请求
    async queryDataAccount() {
      // this.dataValue  日期时间时间戳数组
      let temp = {
        ...this.params,
        ...this.searchForm,
      };
      // console.log(temp);
      // console.log(this.dataValue);
      if (this.dataValue) {
        if (this.dataValue.length > 0) {
          temp.beginTime = this.dataValue[0];
          temp.endTime = this.dataValue[1];
        }
      }
      // console.log(temp);
      for (let key in temp) {
        if (!temp[key]) delete temp[key];
      }
      // console.log(temp);
      // 发送请求
      let res = await pageQueryCustomerAccount(temp);
      // console.log(res);
      this.tableData = res.data.list;
      this.total = res.data.total;
    },
    comment,
    // 每页显示条数发生变化时
    handleSizeChange(val) {
      this.params = {
        page: 1,
        pageSize: val,
      };
    },
    handleCurrentChange(val) {
      this.params.page = val;
    },
  },
  created() {
    this.pageQueryCustomerData();
    this.queryDataAccount();
  },
  mounted() {},
};
</script>
<style scoped>
.page {
  float: right;
  margin-top: 10px;
}
</style>